﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Autocentro.Entities;
using Autocentro.BussinesLogic;

namespace Autocentro.UI
{
    public partial class FrmVehiculo : Form
    {
        [Dependency]
        public IVehiculoBL VehiculoBL { get; set; }

        public FrmVehiculo()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            CargaData();
        }

        private void CargaData()
        {
            vEHICULOBindingSource.DataSource = VehiculoBL.GetFromVehiculosByCriterio(txtCriterio.Text);
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            var frmVehiculo = new FrmMttoVehiculo();
            frmVehiculo.AddVehiculo();
            frmVehiculo.ShowDialog();
            vEHICULOBindingSource.DataSource = VehiculoBL.GetAllFromVehiculos();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var vehiculo = vEHICULOBindingSource.Current as VEHICULO;

            if (vehiculo!=null)
	        {
                var frmVehiculo = new FrmMttoVehiculo();
                frmVehiculo.Editar(vehiculo.Id);
                frmVehiculo.ShowDialog();
                CargaData();
	        }
            else
            {
                MessageBox.Show("Debe seleccionar un vehículo","Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            var vehiculo = vEHICULOBindingSource.Current  as VEHICULO;
            if (vehiculo!=null)
	        {
                var rpt = MessageBox.Show(this,string.Format("¿Desea Eliminar el Vehículo: {0}?", "\nNombre: " + vehiculo.NumSerieMotor+"\nMarca: " + vehiculo.Marca),
                   "Mensaje", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    VehiculoBL.RemoveVehiculo(vehiculo.Id);
                    CargaData();
                    
                }
	        }
            else
            {
                MessageBox.Show("Debe seleccionar un Vehículo", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void FrmVehiculo_Load(object sender, EventArgs e)
        {
            CargaData();
        }

        private void txtCriterio_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CargaData();
            }
        }

        public VEHICULO CurrentVehiculo { get; set; }
        public bool Vehi { get; set; }

        private void vEHICULODataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            CurrentVehiculo = vEHICULOBindingSource.Current as VEHICULO;
            this.Close();
        }

        private void vEHICULODataGridView_DoubleClick(object sender, EventArgs e)
        {
            if (Vehi == true)
            {
                CurrentVehiculo = vEHICULOBindingSource.Current as VEHICULO;
                Close();
            }
        }
    }
}
